package com.skali.cabdirectories.service.persistence;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import com.liferay.portal.kernel.dao.orm.SQLQuery;
import com.liferay.portal.kernel.dao.orm.Session;
import com.liferay.portal.kernel.util.StringBundler;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.kernel.util.StringUtil;
import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
import com.liferay.util.dao.orm.CustomSQLUtil;
import com.skali.cabdirectories.model.CABDirectories_Metadata;
import com.skali.cabdirectories.service.CABDirectories_MetadataLocalServiceUtil;
import com.skali.portal.util.Constants;

public class CABDirectories_MetadataFinderImpl extends BasePersistenceImpl<CABDirectories_Metadata> implements CABDirectories_MetadataFinder{

	public static String FINDBY_CustomCABDirectoriesMetadata_SEARCH = CABDirectories_MetadataFinder.class.getName()+".findByACBNo_OrgName_Field";
	
	
	public List<CABDirectories_Metadata> fetchByCustomCabDirectories_MetadataFields(HashMap<String, Object> customMetadataFieldsMap){

		String CabType = customMetadataFieldsMap.get(Constants.CabType).toString();
		String orgName = customMetadataFieldsMap.get(Constants.OrganizationName).toString();
		String field = customMetadataFieldsMap.get(Constants.Field).toString();
		String samm_no = customMetadataFieldsMap.get(Constants.SammNo).toString();
		String searchKeyword = customMetadataFieldsMap.get(Constants.SearchKeyword).toString();
		System.out.println(" value of searchKeyword recevd in FinderImpl= "+searchKeyword);
		
		boolean isSearchKeywordAvailable = false;
		
		
		Session session = null;
		String sql = StringPool.BLANK;
		String WHERE = StringPool.BLANK;
		StringBundler sb = new StringBundler();
		final String Apostrophe = "'";
		final String Percentage = "%";
		final String OpenParenthesis = "(";
		final String CloseParenthesis = ")";
		String OR_OPERATOR = "OR";
		String AND_OPERATOR = "AND";
		boolean OperatorRequired = false;
		boolean isANDOperatorRequired = false;
		
		int k = 0;
		if(!CabType.isEmpty()) k++;
		if(!orgName.isEmpty()) k++;
		if(!field.isEmpty()) k++;
		if(!samm_no.isEmpty()) k++;
		
		isANDOperatorRequired = (k>=2 || isSearchKeywordAvailable)? true : false;
		if(searchKeyword!=null && !searchKeyword.isEmpty()){
			isSearchKeywordAvailable = true;
		}
	//	System.out.println(">>>>   Inside finder Impl k== "+k);
	//	System.out.println(" value of isANDOperatorRequired = "+isANDOperatorRequired);
		
		List<CABDirectories_Metadata> cabDirectories_MetadataLIST = new ArrayList<CABDirectories_Metadata>();
		try{
			session  = openSession();
			sql = CustomSQLUtil.get(FINDBY_CustomCABDirectoriesMetadata_SEARCH);
			
			if(!CabType.isEmpty()){
				sb.append("WHERE "+OpenParenthesis);
				System.out.println(" inside !CabType.isEmpty() ");
				sb.append("(type_ LIKE "+Apostrophe+CabType+Apostrophe+CloseParenthesis);
			}
			
				if(!orgName.isEmpty() || isSearchKeywordAvailable){
					OperatorRequired = (sb.toString().equalsIgnoreCase("")? false :true);
					sb = (OperatorRequired ? (isANDOperatorRequired ? sb.append(AND_OPERATOR):sb.append("")):(sb.append("")));
					sb.append(OpenParenthesis);
					sb = sb.append(" OrganizationName like "+Apostrophe+Percentage+orgName+Percentage+Apostrophe);
					if(isSearchKeywordAvailable) sb = sb.append(AND_OPERATOR+" OrganizationName like "+Apostrophe+Percentage+searchKeyword+Percentage+Apostrophe);
					sb.append(CloseParenthesis);
				}
				if(!field.isEmpty() || isSearchKeywordAvailable){
					OperatorRequired = (sb.toString().equalsIgnoreCase("")? false :true);
					//	System.out.println(" value of OperatorRequired in field check = "+OperatorRequired);
					/**	 %-- Figure out if there's a need of OR operator at all or NOT...
					 * sb = (OperatorRequired ? (isANDOperatorRequired ? sb.append(AND_OPERATOR):sb.append(OR_OPERATOR)) :(sb.append("")));
					 */				
					sb = (OperatorRequired ? (isANDOperatorRequired ? sb.append(AND_OPERATOR):sb.append("")) :(sb.append("")));
					sb.append(OpenParenthesis);
					sb.append("field LIKE "+Apostrophe +Percentage+field+Percentage+Apostrophe);
					if(isSearchKeywordAvailable) sb = sb.append(AND_OPERATOR+" field LIKE "+Apostrophe+Percentage+searchKeyword+Percentage+Apostrophe);
					sb.append(CloseParenthesis);
				}
				if(!samm_no.isEmpty() || isSearchKeywordAvailable){
					OperatorRequired = (sb.toString().equalsIgnoreCase("")? false :true);
					sb = (OperatorRequired ? (isANDOperatorRequired ? sb.append(AND_OPERATOR):sb.append("")):(sb.append("")));
					sb.append(OpenParenthesis);
					sb.append(" sammno LIKE "+Apostrophe+Percentage+samm_no+Percentage+Apostrophe);
					if(isSearchKeywordAvailable) sb = sb.append(AND_OPERATOR+" field LIKE "+Apostrophe+Percentage+searchKeyword+Percentage+Apostrophe);
					sb.append(CloseParenthesis);
				}

			if(!sb.toString().equalsIgnoreCase("")){
				sb.append(CloseParenthesis);
			}
			
			WHERE = sb.toString();
			sql = StringUtil.replace(sql, "[$WHERE$]", WHERE);
			
			SQLQuery sqlQ = session.createSQLQuery(sql);
			System.out.println(">>> sql generated - inside finder impl <<<<<"+sql);
			
			List list = sqlQ.list();
			Iterator it = list.iterator();
			
			while(it.hasNext()){
				CABDirectories_Metadata cabDirectories_Metadata = null;  
				Object[] obj = (Object[])it.next();
				for(int i=0;i<obj.length;i++){
				//	System.out.println("response frm FinderImpl --- at "+i +"th posn  = "+obj[i]);
					long sammno = 0;	//(Long)obj[0];
					sammno = Long.valueOf(obj[0].toString());
					cabDirectories_Metadata = CABDirectories_MetadataLocalServiceUtil.getCABDirectories_Metadata(sammno);
					
				}
				cabDirectories_MetadataLIST.add(cabDirectories_Metadata);
			}	
		}catch(Exception e){
			e.printStackTrace();
		}		
		return cabDirectories_MetadataLIST;

	}
}
